package com.kylin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    //查询数据看库的所有信息
    //没有实体类，数据库中的东西，怎么获取？Map
    @GetMapping("/userlist")
    public List<Map<String,Object>> userList(){
        String sql = "select * from users";
        List<Map<String,Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }
    @GetMapping("/adduser")
    public String addUser(){
        String sql = "insert into users(id,username,password) values(default,'ls','444')";
        jdbcTemplate.update(sql);
        return "update-ok";
    }
    @GetMapping("/updateuser/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql = "update users set username=?,password=? where id="+id;

        //封装
        Object[] objects = new Object[2];
        objects[0]="赵云";
        objects[1]="zy";
        jdbcTemplate.update(sql,objects);
        return "updateUser-ok";
    }
    @GetMapping("/deleteuser/{id}")
    public String deleteUser(@PathVariable("id") int id){

        String sql="delete from users where id =?";
        jdbcTemplate.update(sql,id);
        return "deleteUser-ok";
    }
}
